package xyz.kongbai121.shop.member.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.kongbai121.common.utils.PageUtils;
import xyz.kongbai121.common.utils.Query;

import xyz.kongbai121.shop.member.dao.MemberDao;
import xyz.kongbai121.shop.member.dao.MemberLevelDao;
import xyz.kongbai121.shop.member.entity.MemberEntity;
import xyz.kongbai121.shop.member.entity.MemberLevelEntity;
import xyz.kongbai121.shop.member.exception.PhoneExistException;
import xyz.kongbai121.shop.member.exception.UsernameExistException;
import xyz.kongbai121.shop.member.service.MemberService;
import xyz.kongbai121.shop.member.vo.GiteeUserInfo;
import xyz.kongbai121.shop.member.vo.MemberLoginVo;
import xyz.kongbai121.shop.member.vo.MemberRegistVo;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        MemberEntity entity = new MemberEntity();
        //检查用户名和手机号是否唯一
        checkPhoneUnique(vo.getPhone());
        checkUserNameUnique(vo.getUserName());
        entity.setUsername(vo.getUserName());
        entity.setMobile(vo.getPhone());
        entity.setNickname(vo.getUserName());
        //密码加密保存
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        entity.setPassword(encode);
        // 默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        entity.setLevelId(levelEntity.getId());
        this.baseMapper.insert(entity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException{
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (count>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUserNameUnique(String username) throws UsernameExistException{
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if (count>0) {
            throw new UsernameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {

        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();

        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",loginacct).or().eq("mobile",loginacct);
        MemberEntity entity = this.baseMapper.selectOne(wrapper);
        if (entity == null){
            //登录失败
            return null;
        }else {
            //数据库密码
            String passwordDb = entity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            //密码匹配
            boolean matches = passwordEncoder.matches(password, passwordDb);
            if (matches){
                return entity;
            }else{
                return null;
            }
        }

    }

    @Override
    public MemberEntity login(GiteeUserInfo giteeUserInfo) {
        //登录和登录合并逻辑
        MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", giteeUserInfo.getId()));
        if (memberEntity != null){
            MemberEntity entity = new MemberEntity();
            entity.setId(memberEntity.getId());
            entity.setAccessToken(giteeUserInfo.getAccess_token());
            entity.setExpiresIn(String.valueOf(giteeUserInfo.getExpires_in()));
            this.baseMapper.updateById(entity);

            memberEntity.setAccessToken(giteeUserInfo.getAccess_token());
            memberEntity.setExpiresIn(String.valueOf(giteeUserInfo.getExpires_in()));
            return memberEntity;
        }else {
            MemberEntity entity = new MemberEntity();
            entity.setSocialUid(String.valueOf(giteeUserInfo.getId()));
            entity.setAccessToken(giteeUserInfo.getAccess_token());
            entity.setExpiresIn(String.valueOf(giteeUserInfo.getExpires_in()));
            entity.setNickname(giteeUserInfo.getName());
            this.baseMapper.insert(entity);
            return entity;
        }
    }

}